package com.ze.sys.excel;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ze.page.PageResponse;
import com.ze.sys.entity.SysUser;
import com.ze.sys.entity.req.PageSysUserReq;
import com.ze.sys.service.SysUserService;
import com.ze.tool.excel.BaseEasyExcelExport;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class SysUserExcelExport extends BaseEasyExcelExport<SysUser, SysUser> {


    @Resource
    private SysUserService sysUserService;

    /**
     * 数据导出
     */
    public void exportWithBigData(String fileName, Map<String, Object> conditions) {
        this.exportExcel(fileName, conditions);
    }

    @Override
    protected List<List<String>> getExcelHead() {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("用户编号"));
        head.add(Collections.singletonList("用户姓名"));
        return head;
    }

    @Override
    protected Long dataTotalCount(Map<String, Object> conditions) {
        return sysUserService.queryCount(conditions);
    }

    @Override
    protected void buildDataList(List<List<String>> resultList, Map<String, Object> condition,
                                 Long pageNo, Long pageSize) {
        PageSysUserReq pageSysUserReq = new PageSysUserReq();
        //可以根据condition设置条件
        pageSysUserReq.setPageNo(pageNo);
        pageSysUserReq.setPageSize(pageSize);
        PageResponse<SysUser> pageResponse = sysUserService.queryByPage(pageSysUserReq);
        List<SysUser> sysUserList = pageResponse.getResult();
        if (CollectionUtils.isNotEmpty(sysUserList)) {
            sysUserList.forEach(sysUser -> {
                resultList.add(Arrays.asList(sysUser.getId().toString(), sysUser.getName()));
            });
        }
    }
}
